﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;

    public abstract class ClientObjectCollection : ClientObject, IEnumerable
    {
        private List<object> m_data;
        private bool m_dataInited;

        protected ClientObjectCollection(ClientRuntimeContext context, ObjectPath objectPath) : base(context, objectPath)
        {
        }

        protected void AddChild(ClientObject obj)
        {
            this.Data.Add(obj);
            if (obj.ParentCollection == null)
            {
                obj.ParentCollection = this;
            }
            this.m_dataInited = true;
        }

        protected object GetItemAtIndex(int i)
        {
            return this.Data[i];
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            if (!(peekedName == "_Child_Items_"))
            {
                return base.InitOnePropertyFromJson(peekedName, reader);
            }
            reader.ReadName();
            this.m_data = new List<object>();
            this.m_dataInited = true;
            object[] objArray = reader.ReadObjectArray();
            if (objArray != null)
            {
                for (int i = 0; i < objArray.Length; i++)
                {
                    ClientObject obj2 = objArray[i] as ClientObject;
                    if (obj2 != null)
                    {
                        obj2.ParentCollection = this;
                    }
                    this.m_data.Add(objArray[i]);
                }
            }
            return true;
        }

        protected internal void RemoveChild(ClientObject obj)
        {
            if (this.m_data != null)
            {
                ObjectPathIdentity path = null;
                path = obj.Path as ObjectPathIdentity;
                for (int i = this.m_data.Count - 1; i >= 0; i--)
                {
                    ClientObject obj2 = null;
                    ObjectPathIdentity identity2 = null;
                    if (this.m_data[i] == obj)
                    {
                        if (((ClientObject) this.m_data[i]).ParentCollection == this)
                        {
                            ((ClientObject) this.m_data[i]).ParentCollection = null;
                        }
                        this.m_data.RemoveAt(i);
                    }
                    else if (((path != null) && ((obj2 = this.m_data[i] as ClientObject) != null)) && (((identity2 = obj2.Path as ObjectPathIdentity) != null) && (path.Identity == identity2.Identity)))
                    {
                        if (((ClientObject) this.m_data[i]).ParentCollection == this)
                        {
                            ((ClientObject) this.m_data[i]).ParentCollection = null;
                        }
                        this.m_data.RemoveAt(i);
                    }
                }
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            this.ThrowIfCollectionNotInited();
            int count = this.Data.Count;
            int i = 0;
            while (true)
            {
                if (i >= this.Data.Count)
                {
                    yield break;
                }
                if (count != this.Data.Count)
                {
                    throw new InvalidOperationException(Resources.GetString("CollectionModified"));
                }
                yield return this.GetItemAtIndex(i);
                i++;
            }
        }

        internal void ThrowIfCollectionNotInited()
        {
            if (!this.m_dataInited)
            {
                throw new CollectionNotInitializedException();
            }
        }

        public virtual int Count
        {
            get
            {
                this.ThrowIfCollectionNotInited();
                return this.Data.Count;
            }
        }

        protected List<object> Data
        {
            get
            {
                if (this.m_data == null)
                {
                    this.m_data = new List<object>();
                }
                return this.m_data;
            }
        }

        //[CompilerGenerated]
        //private sealed class GetEnumerator>d__0 : IEnumerator<object>, IEnumerator, IDisposable
        //{
        //    private int <>1__state;
        //    private object <>2__current;
        //    public ClientObjectCollection <>4__this;
        //    public int <i>5__2;
        //    public int <originalCount>5__1;

        //    [DebuggerHidden]
        //    public GetEnumerator>d__0(int <>1__state)
        //    {
        //        this.<>1__state = <>1__state;
        //    }

        //    private bool MoveNext()
        //    {
        //        switch (this.<>1__state)
        //        {
        //            case 0:
        //                this.<>1__state = -1;
        //                this.<>4__this.ThrowIfCollectionNotInited();
        //                this.<originalCount>5__1 = this.<>4__this.Data.Count;
        //                this.<i>5__2 = 0;
        //                break;

        //            case 1:
        //                this.<>1__state = -1;
        //                this.<i>5__2++;
        //                break;

        //            default:
        //                goto Label_00C0;
        //        }
        //        if (this.<i>5__2 < this.<>4__this.Data.Count)
        //        {
        //            if (this.<originalCount>5__1 != this.<>4__this.Data.Count)
        //            {
        //                throw new InvalidOperationException(Resources.GetString("CollectionModified"));
        //            }
        //            this.<>2__current = this.<>4__this.GetItemAtIndex(this.<i>5__2);
        //            this.<>1__state = 1;
        //            return true;
        //        }
        //    Label_00C0:
        //        return false;
        //    }

        //    [DebuggerHidden]
        //    void IEnumerator.Reset()
        //    {
        //        throw new NotSupportedException();
        //    }

        //    void IDisposable.Dispose()
        //    {
        //    }

        //    object IEnumerator<object>.Current
        //    {
        //        [DebuggerHidden]
        //        get
        //        {
        //            return this.<>2__current;
        //        }
        //    }

        //    object IEnumerator.Current
        //    {
        //        [DebuggerHidden]
        //        get
        //        {
        //            return this.<>2__current;
        //        }
        //    }
        //}
    }
}

